package com.glela.order.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.glela.common.constant.OrderStatusEnum;
import com.glela.common.util.DateUtil;
import com.glela.order.dao.OrderDao;
import com.glela.order.mapper.OrderDetailMapper;
import com.glela.order.model.LogisticsStatus;
import com.glela.order.model.OrderDetail;
import com.glela.order.model.OrderStatus;
import com.glela.order.model.logisticsDetail;
import com.glela.order.service.LogisticsStatusService;
import com.glela.order.service.OrderStatusService;

/**
 * 状态机
 * 
 * 只有单一的职责 仅仅用于订单状态的修改  负责维护订单数据的完整性  确保订单状态始终保持健康的状态
 * 
 * @author MaoZeYao
 * 2017年4月21日 下午1:44:04
 *
 */
@Service("orderStatusService")
@Scope("singleton")
public class OrderStatusServiceImpl implements OrderStatusService {
	
	@Autowired
	protected OrderDao orderDao;

	@Autowired
	protected OrderDetailMapper orderDetailMapper;
	/*查看物流信息*/
	@Autowired
	protected LogisticsStatusService logisticsStatusService;
	
	private static Logger logger = LoggerFactory.getLogger(OrderStatusServiceImpl.class);
	/**
	 * 支付成功
	 */
	@Override
	public int successPayment(String orderObjectSn) {
		// TODO Auto-generated method stub
		return orderDao.updateOrderStatusSuccess(orderObjectSn);
	}

	/**
	 * 订单发货接口
	 * 原则：对退款订单不做处理
	 * OrderStatusEnum.S2.getCode();
	 * OrderStatusEnum.S3.getCode();
	 */
	@Override
	public int orderDelivery(String orderInfoSn) {
		// TODO Auto-generated method stub
		try {
			String orderObjectSn =  orderDao.selectOrderObjectSnByOrderInfoSn(orderInfoSn);
			List<OrderStatus> sList = orderDao.selectForStatusUserOrderObjectSn(orderObjectSn);
			long orderObjectId 	= 0;
			long orderInfoId 	= 0;
			boolean upObj 		= false;
			// 单独运算订单明细
			for (OrderStatus allStatus : sList) {
				if (orderInfoSn.equals(allStatus.getOrderInfoSn())) {
					orderInfoId = allStatus.getOrderInfoId();
					if (allStatus.getInfoStatus() == OrderStatusEnum.S2.getCode()) {
						allStatus.setInfoStatus(OrderStatusEnum.S3.getCode());
					}
					if (allStatus.getDetailStatus() == OrderStatusEnum.S2.getCode()) {
						allStatus.setDetailStatus(OrderStatusEnum.S3.getCode());
					}
				}
			}
			
			orderDao.updateOrderInfoStatusById(orderInfoId,OrderStatusEnum.S3.getCode(),OrderStatusEnum.S2.getCode());
			
			orderDao.updateOrderDetailStatusByOrderInfoId(orderInfoId,OrderStatusEnum.S3.getCode(),OrderStatusEnum.S2.getCode());
			
			for (OrderStatus allStatus : sList) {
				if (allStatus.getDetailStatus() == OrderStatusEnum.S2.getCode()) {
					upObj = false;
					break;
				} else {
					orderObjectId = allStatus.getOrderObjectId();
					upObj = true;
				}
			}
			
			if (upObj) {
				orderDao.updateOrderObjectStatusById(orderObjectId,OrderStatusEnum.S3.getCode(),OrderStatusEnum.S2.getCode());
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public int confirmOrder(String orderInfoSn) {
		try {
			//查看订单的物流信息
			LogisticsStatus  logisticsStatus=logisticsStatusService.getLogisticsPojo(orderInfoSn);
			String orderObjectSn =  orderDao.selectOrderObjectSnByOrderInfoSn(orderInfoSn);
			List<OrderStatus> sList = orderDao.selectForStatusUserOrderObjectSn(orderObjectSn);
			long orderObjectId 	= 0;
			long orderInfoId 	= 0;
			boolean upObj 		= false;
			// 单独运算订单明细
			for (OrderStatus allStatus : sList) {
				if (orderInfoSn.equals(allStatus.getOrderInfoSn())) {
					orderInfoId = allStatus.getOrderInfoId();
					if (allStatus.getInfoStatus() == OrderStatusEnum.S3.getCode()) {
						allStatus.setInfoStatus(OrderStatusEnum.S4.getCode());
					}
					if (allStatus.getDetailStatus() == OrderStatusEnum.S3.getCode()) {
						allStatus.setDetailStatus(OrderStatusEnum.S4.getCode());
					}
				}
			}
			//更新子单和明细单的状态
			orderDao.updateOrderInfoStatusById(orderInfoId,OrderStatusEnum.S4.getCode(),OrderStatusEnum.S3.getCode());
			orderDao.updateOrderDetailStatusByOrderInfoId(orderInfoId,OrderStatusEnum.S4.getCode(),OrderStatusEnum.S3.getCode());
			//设置收货时间 如果没有就设置为当前时间  begin
			if(logisticsStatus!=null&&logisticsStatus.getIsFinish()!=null&&logisticsStatus.getIsFinish()==1){//已经确认收货
				List<logisticsDetail> list=JSONObject.parseArray(
						JSONObject.parseObject(logisticsStatus.getLogisticsSessage()).get("list").toString(), logisticsDetail.class);
			orderDao.updateConfirmTime(orderInfoId,list.get(list.size()-1).getDatetime());
			}else{
				orderDao.updateConfirmTime(orderInfoId,DateUtil.getNow());
			}
			//设置收货时间  如果没有就设置为当前时间 end
			for (OrderStatus allStatus : sList) {
				if (allStatus.getDetailStatus() == OrderStatusEnum.S3.getCode()) {
					upObj = false;
					break;
				} else {
					orderObjectId = allStatus.getOrderObjectId();
					upObj = true;
				}
			}
			
			if (upObj) {
				orderDao.updateOrderObjectStatusById(orderObjectId,OrderStatusEnum.S4.getCode(),OrderStatusEnum.S3.getCode());
			}
			return 1;
		} catch (Exception e) {
			logger.error("订单确认收货失败 订单号:{} {}",orderInfoSn,e);
			e.printStackTrace();
			return 0;
		}
	}

	/*@Override
	public int cancelOrder(String orderObjectSn) {
		// TODO Auto-generated method stub
		return 0;
	}*/

	@Override
	public int closeOrder(OrderDetail orderDetail) {
		// TODO Auto-generated method stub
		try {
			String orderObjectSn = orderDao.selectOrderObjectSnById(orderDetail.getOrderObjectId());
			List<OrderStatus> sList = orderDao.selectForStatusUserOrderObjectSn(orderObjectSn);
			long orderInfoId = orderDetail.getOrderId();
			long orderObjectId = orderDetail.getOrderObjectId();
			int infoStatus = 0;
			int objStatus = 0;
			boolean upObj = false;
			boolean upinfo = false;
			// 单独运算订单明细
			orderDao.updateOrderDetailStatusById(orderDetail.getId(), OrderStatusEnum.S7.getCode(),
					orderDetail.getStatus());
			
			if (sList.size() == 1) {
				orderDao.updateOrderInfoStatusById(orderInfoId,OrderStatusEnum.S7.getCode(),sList.get(0).getInfoStatus());
				orderDao.updateOrderObjectStatusById(orderObjectId,OrderStatusEnum.S7.getCode(),sList.get(0).getObjectStatus());
				return 1;
			}
			
			for (OrderStatus allStatus : sList) {
				if (allStatus.getOrderInfoId() == orderDetail.getOrderId()) {
					if (allStatus.getOrderDetailId() == orderDetail.getId()) {
						allStatus.setDetailStatus(OrderStatusEnum.S7.getCode());
					}
					if (allStatus.getDetailStatus() != OrderStatusEnum.S7.getCode()) {
						upinfo = false;
						break;
					} else {
						if (allStatus.getOrderDetailId() == orderDetail.getId()) {
							infoStatus = allStatus.getInfoStatus();
						}
						allStatus.setInfoStatus(OrderStatusEnum.S7.getCode());
						upinfo = true;
					}
				}
			}
			
			if (upinfo) {
				orderDao.updateOrderInfoStatusById(orderInfoId,OrderStatusEnum.S7.getCode(),infoStatus);
				for (OrderStatus allStatus : sList) {
					if (allStatus.getInfoStatus() != OrderStatusEnum.S7.getCode()) {
						upObj = false;
						return 10000;
					} else {
						objStatus = allStatus.getObjectStatus();
						upObj = true;
					}
				}
				if (upObj) {
					orderDao.updateOrderObjectStatusById(orderObjectId,OrderStatusEnum.S7.getCode(),objStatus);
				}
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	
}
